package com.personal.common.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONUtil {
	public static Map<String, Object> json2Map(String strJson) {
		JSONObject jsonObject = JSONObject.fromObject(strJson);

		Map<String, Object> map = Maps.newHashMap();
		for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
			String key = (String) iter.next();
			map.put(key, jsonObject.get(key));
		}
		return map;
	}

	/***
	 * 返回Map（String，String） 类型
	 * 
	 * @param strJosn
	 * @return
	 */
	public static List<Map<String, Object>> json2List(String strJosn) {
		List<Map<String, Object>> list = Lists.newArrayList();
		JSONArray jsonArray = JSONArray.fromObject(strJosn);
		list = JSONArray.toList(jsonArray, Map.class);

		return list;
	}
	
	public static List<Map<String, String>> json2MapStringList(String strJosn) {
		List<Map<String, String>> list = Lists.newArrayList();
		JSONArray jsonArray = JSONArray.fromObject(strJosn);
		list = JSONArray.toList(jsonArray, Map.class);

		return list;
	}

	public static Map<String, String> json2MapStr(String strJson) {
		JSONObject jsonObject = JSONObject.fromObject(strJson);

		Map<String, String> map = Maps.newHashMap();
		for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
			String key = (String) iter.next();
			map.put(key, String.valueOf(jsonObject.get(key)));
		}
		return map;
	}

	public static List<String> json2ListString(String strJson) {
		List<String> list = Lists.newArrayList();
		JSONArray jsonArray = JSONArray.fromObject(strJson);
		list = JSONArray.toList(jsonArray, String.class);
		return list;
	}



}
